package x.ovo.wechat.bot.http.request.contact;

import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.json.JSONUtil;
import x.ovo.wechat.bot.common.Constant;
import x.ovo.wechat.bot.common.http.request.ApiRequest;
import x.ovo.wechat.bot.common.http.session.LoginSession;

import java.util.Map;
import java.util.function.Function;

/**
 * 修改联系人备注请求
 *
 * @author ovo on 2024/05/06.
 * @since 1.0.0
 */
public class ModifyContactRemarkRequest extends ApiRequest<Boolean> {

    private final String remarkName;
    private final String username;

    public ModifyContactRemarkRequest(LoginSession session, String remarkName, String username) {
        super(session);
        this.remarkName = remarkName;
        this.username = username;
    }

    @Override
    public String getUrl() {
        return UrlBuilder.of(this.session.getUrl())
                .addPath(Constant.MODIFY_CONTACT_REMARK)
                .build();
    }

    @Override
    public Map<String, Object> getParameters() {
        return JSONUtil.createObj()
                .set("CmdId", 2)
                .set("RemarkName", remarkName)
                .set("UserName", username)
                .getRaw();
    }

    @Override
    public String getMethod() {
        return POST;
    }

    @Override
    public boolean isJson() {
        return true;
    }

    @Override
    public Function<String, Boolean> responseHandler() {
        return s -> JSONUtil.parseObj(s).getJSONObject("BaseResponse").getInt("Ret") == 0;
    }
}
